﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fashion.Common.Const;
using Fashion.Models;
using Fashion.Models.Interface;
using Fashion.Models.Model;

namespace Fashion.Controllers
{
    public class ProductController : BaseController
    {
        private IProductRepository _repository;
        public ProductController()
        {
            _repository = new ProductRepository();
        }

        #region Product
        
        public ActionResult Index(int categoryId=15)
        {
            ViewData[ViewDataName.CategoryId] = categoryId;
            return View(_repository.GetAllByCategoryId(categoryId));
        }

        public ActionResult Create(int categoryId)
        {
            ProductModel model = new ProductModel();
            model.CategoryId = categoryId;
            model.Currency = "VNĐ";
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(ProductModel model)
        {
            if (!ModelState.IsValid)
                return View(model);
            Product product = new Product
                                  {
                                      CategoryId = model.CategoryId,
                                      Name = model.Name,
                                      Description = model.Description,
                                      Price = model.Price,
                                      Currency = model.Currency,
                                      Quantity = model.Quantity,
                                      CountView = 0,
                                      DateTimeCreate = DateTime.Now,
                                      Active = true,
                                  };
            _repository.AddNewProduct(product);
            return RedirectToAction("Index", new {categoryId = product.CategoryId});
        }

        public ActionResult Update(int id)
        {
            Product product = _repository.GetProductById(id);
            var model = new ProductModel()
                            {
                                CategoryId = product.CategoryId,
                                Name = product.Name,
                                Description = product.Description,
                                Price = (int)product.Price,
                                Currency = product.Currency,
                                Quantity = product.Quantity,
                            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Update(ProductModel model)
        {
            if (!ModelState.IsValid)
                return View(model);
            var product = _repository.GetProductById(model.Id);
            product.Name = model.Name;
            product.Description = model.Description;
            product.Price = model.Price;
            product.Currency = model.Currency;
            product.Quantity = model.Quantity;
            _repository.Update();
            return RedirectToAction("Index", new { categoryId = model.CategoryId });
        }

        public ActionResult Delete(int id, int categoryId)
        {
            _repository.Delete(id);
            return RedirectToAction("Index", new {categoryId = categoryId});
        }

        public ActionResult Detail(int id)
        {
            var product = _repository.GetProductById(id);
            return View(product);
        }

        #endregion

        #region Product Image
        
        [Authorize]
        public ActionResult ViewImages(int productId)
        {
            var product = _repository.GetProductById(productId);
            ViewData[ViewDataName.ProductId] = productId;
            return View(product.ProductImages);
        }

        [HttpPost]
        public ActionResult Upload(int productId)
        {
            foreach (string file in Request.Files)
            {
                var postedFile = Request.Files[file];
                if (postedFile != null && !string.IsNullOrEmpty(postedFile.FileName))
                {
                    string extension = Path.GetExtension(postedFile.FileName);
                    string filePath = _repository.UploadImage(productId,extension);
                    postedFile.SaveAs(Server.MapPath(filePath));
                }
            }
            return RedirectToAction("ViewImages", new { productId = productId});
        }

        public ActionResult DeleteImage(int id, int productId)
        {
            _repository.DeleteImage(id);
            return RedirectToAction("ViewImages", new { productId = productId });
        }

        public ActionResult SetMainImage(int id, int productId)
        {
            _repository.SetMainImage(id,productId);
            return RedirectToAction("ViewImages", new { productId = productId });
        }

        #endregion
    }
}
